package com.android.prpltalk;

import edu.stanford.prpl.api.BlobResource;
import edu.stanford.prpl.api.Identity;
import edu.stanford.prpl.app.prpltalk.PRPLTalk.BuddyInfo;

/**
* This class represents a generic contact that can be online or offline.
* Each contact has a name and a mandatory email. Please note that 
* the Agent shall be identified with the email.
* It seems that the emulator does not support a way to customize the 
* phone number. So for now the application reads the number from a 
* property file.
* Each contact has non null location:  online contacts have a location 
* that is updated while offline contacts have a fixed location 
* (null location)(they should not be drawn).
* <p>
* This class is almost immutable. The <code>isOnline</code> flag is 
* the only mutable field
*
*/

public class Contact
{
	/** 
	 * The contact phone number. 
	 */
	private final String email;	
	
	/** 
	 * Status of the contact. 
	 * This flag is mutable 
	 */
	private volatile boolean isOnline;
	
	/** 
	 * The contact name, the email address otherwise. 
	 */
	private final String name; 
	
	/** 
	 * True if the contact is present in local phone contacts, false otherwise. 
	 */
	private final boolean storedOnPhone;
	
	private BuddyInfo contactPrPlInfo;
	

	
	/**
	 * Instantiates a new contact.
	 * 
	 * @param b the contact to be copied
	 * @param stored true if contact is stored on phone, false otherwise
	 */
	public Contact(BuddyInfo b, boolean stored)
	{
		this.name = b.name;
		this.email = b.identity.getKey();
		this.isOnline = false;
		this.storedOnPhone = stored;
		
		this.contactPrPlInfo = b;
	}	
	
	/**
	 * Performs a deep copy of a contact.
	 * 
	 * @param c the contact to be copied
	 */
	public Contact(Contact c)
	{
		this.name = c.name;
		this.email = c.email;
		this.isOnline = c.isOnline;
		this.storedOnPhone = c.storedOnPhone;
		
		this.contactPrPlInfo = c.contactPrPlInfo;
	}
	
	// Dangerous to use
	public Contact(String name, String email, boolean stored)
	{
		this.name = name;
		this.email = email;
		this.isOnline = false;
		this.storedOnPhone = stored;
		
		this.contactPrPlInfo = null;
	}	
	
	
	/**
	 * Gets the contact's email.
	 * 
	 * @return the contact's email
	 */
	public String getEmail()
	{
		return this.email;
	}
	

	/**
	 * Gets the contact name.
	 * 
	 * @return the contact name
	 */
	public String getName() 
	{
		return this.name;
	}
	
	/**
	 * Checks if a contact is stored on phone.
	 * 
	 * @return true if it is stored on phone, false otherwise
	 */
	public boolean isStoredOnPhone()
	{
		return storedOnPhone;
	}

	/**
	 * Prints out the contact name
	 * 
	 *  @return the contact name
	 */
	public String toString()
	{
		return this.name;
	}
	
	/**
	 * Change contact status to online.
	 */
	public void setOnline()
	{
		this.isOnline = true;
	}
	
	/**
	 * Change contact status to offline.
	 */
	public void setOffline()
	{
		this.isOnline = false;
	}
	
	/**
	 * Checks if contact is online.
	 * 
	 * @return true if online, false otherwise
	 */
	public boolean isOnline()
	{
		return this.isOnline;
	}

	
	/**
	 * Checks for contacts equality
	 * 
	 *  @param o the object to be checked
	 *  @return true if contacts have the same phone number, false otherwise
	 */
	public boolean equals(Object o)
	{	
		boolean res = false;
		
		if (o instanceof Contact) 
		{
			Contact other = (Contact) o;					
			res = this.email.equals(other.email);	
		}
		
		return res;
	}	

}